<?php  
namespace app\home\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Cookie;
class Userorder extends Controller
{
    // 我的订单
    public function usercenter()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            $request = Request::instance();
            // 当前页
            $page = $request->get('page',1);
            // 每页显示条数
            $pagesize = 10;
            // 偏移量
            $offest = ($page-1)*$pagesize;
            $sql = "select count(*) as num from man_order where u_id=$u_id and u_del=0";
            $res = Db::query($sql);
            // 尾页
            $lastpage = ceil($res[0]['num']/$pagesize);
            // 上一页
            $uppage = $page -  1 < 1 ? 1 :$page-1;
            // 下一页
            $downpage = $page + 1> $lastpage ? $lastpage : $page+1;
            $page = array(
                'page'=>$page,
                'uppage'=>$uppage,
                'downpage'=>$downpage,
                'lastpage'=>$lastpage
            );
            // 查询全部未删除订单
            $sql = "select * from man_order where u_id=$u_id and u_del=0 order by o_addtime desc limit $offest,$pagesize";
            $orderData = Db::query($sql);

            $typeData = $this->alltype();
            return view('usercenter',[
                'typeData'=>$typeData,
                'orderData'=>$orderData,
                'page'=>$page
                ]
            );
        }else{
            return redirect("user/login");
        }
    }
    // 用户订单删除
    public function userOderDel()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            $o_id = input("get.o_id");
            if(!empty($o_id)){
                // 查询全部订单
                $sql = "update man_order set u_del=1 where o_id=$o_id and u_id=$u_id";
                Db::execute($sql);
                echo "0";
            }else{
                echo "2";
            }
        }else{
            echo "1";
        }
    }
    // 代付款
    public function userpayment()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            $request = Request::instance();
            // 当前页
            $page = $request->get('page',1);
            // 每页显示条数
            $pagesize = 10;
            // 偏移量
            $offest = ($page-1)*$pagesize;
            $sql = "select count(*) as num from man_order where u_id=$u_id and u_del=0 and o_statc=1";
            $res = Db::query($sql);
            // 尾页
            $lastpage = ceil($res[0]['num']/$pagesize);
            // 上一页
            $uppage = $page -  1 < 1 ? 1 :$page-1;
            // 下一页
            $downpage = $page + 1> $lastpage ? $lastpage : $page+1;
            $page = array(
                'page'=>$page,
                'uppage'=>$uppage,
                'downpage'=>$downpage,
                'lastpage'=>$lastpage
            );
            // 查询全部未付款订单
            $sql = "select * from man_order where u_id=$u_id and u_del=0 and o_statc=1 order by o_addtime desc limit $offest,$pagesize";
            $orderData = Db::query($sql);

            $typeData = $this->alltype();
            return view('userpayment',[
                'typeData'=>$typeData,
                'orderData'=>$orderData,
                'page'=>$page
                ]
            );
        }else{
            return redirect("user/login");
        }
    }
    // 代发货
    public function userdropship()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            $request = Request::instance();
            // 当前页
            $page = $request->get('page',1);
            // 每页显示条数
            $pagesize = 10;
            // 偏移量
            $offest = ($page-1)*$pagesize;
            $sql = "select count(*) as num from man_order where u_id=$u_id and u_del=0 and o_statc=2";
            $res = Db::query($sql);
            // 尾页
            $lastpage = ceil($res[0]['num']/$pagesize);
            // 上一页
            $uppage = $page -  1 < 1 ? 1 :$page-1;
            // 下一页
            $downpage = $page + 1> $lastpage ? $lastpage : $page+1;
            $page = array(
                'page'=>$page,
                'uppage'=>$uppage,
                'downpage'=>$downpage,
                'lastpage'=>$lastpage
            );
            // 查询全部未付款订单
            $sql = "select * from man_order where u_id=$u_id and u_del=0 and o_statc=2 order by o_addtime desc limit $offest,$pagesize";
            $orderData = Db::query($sql);

            $typeData = $this->alltype();
            return view('userdropship',[
                'typeData'=>$typeData,
                'orderData'=>$orderData,
                'page'=>$page
                ]
            );
        }else{
            return redirect("user/login");
        }
    }
    // 代评价
    public function userremain()
    {
        $u_id = Cookie::get('u_id');
        if(!empty($u_id)){
            $request = Request::instance();
            // 当前页
            $page = $request->get('page',1);
            // 每页显示条数
            $pagesize = 10;
            // 偏移量
            $offest = ($page-1)*$pagesize;
            $sql = "select count(*) as num from man_order where u_id=$u_id and u_del=0 and o_statc=4 and o_remark=''";
            $res = Db::query($sql);
            // 尾页
            $lastpage = ceil($res[0]['num']/$pagesize);
            // 上一页
            $uppage = $page -  1 < 1 ? 1 :$page-1;
            // 下一页
            $downpage = $page + 1> $lastpage ? $lastpage : $page+1;
            $page = array(
                'page'=>$page,
                'uppage'=>$uppage,
                'downpage'=>$downpage,
                'lastpage'=>$lastpage
            );
            // 查询全部未评价订单
            $sql = "select * from man_order where u_id=$u_id and u_del=0 and o_statc=4 and o_remark='' order by o_addtime desc limit $offest,$pagesize";
            $orderData = Db::query($sql);

            $typeData = $this->alltype();
            return view('userremain',[
                'typeData'=>$typeData,
                'orderData'=>$orderData,
                'page'=>$page
                ]
            );
        }else{
            return redirect("user/login");
        }
    }

    // 分类查询
    public function alltype()
    {
        // 查询分类
        $sql = "select * from man_category";
        $typeData = Db::query($sql);
        $typeData = $this->createTreeBySon($typeData);
        return $typeData;
    }

    // 多级分类递归方法  多维数组
    public function createTreeBySon($info,$c_pid=0)
    {
        // 定义数组
        $newArr = array();
        // 处理
        foreach ($info as $k=>$v) {
            // 判断
            if($v['c_pid'] == $c_pid){

                // 把v赋给新数组追加一个下标
                $newArr[$k] = $v;

                // 调用自己 查找所有子辈放到son下
                $newArr[$k]['son'] = $this->createTreeBySon($info,$v['c_id']);
            }
        }
        // 数组返回
        return $newArr;
    }
}